<template>
    <!-- 搜索表 -->
    <el-form :inline="true" :model="formInline" class="w-full h-10">
        <el-form-item label="姓名">
            <el-input v-model="formInline.name" placeholder="姓名" />
        </el-form-item>
        <el-form-item label="电话">
            <el-input v-model="formInline.phonenumber" placeholder="电话" />
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="onSearch">搜索</el-button>
        </el-form-item>
    </el-form>
    <div class="w-full">
        <!-- 表格显示 -->
        <el-table :data="tableData" style="width: 100%" border height="60vh" highlight-current-row @row-dblclick="getInfo" @selection-change="selected">
            <el-table-column fixed type="selection" width="50" />
            <el-table-column prop="patientId" label="序号" width="80" align="center"></el-table-column>
            <el-table-column prop="name" label="姓名"  align="center"></el-table-column>
            <el-table-column prop="sex" label="性别"  align="center">
                <template #default="scope">
                    {{ scope.row.sex == 0 ? "男" : "女" }}
                </template>
            </el-table-column>
            <el-table-column prop="email" label="邮箱"  align="center"></el-table-column>
            <el-table-column prop="phonenumber" label="电话"  align="center"></el-table-column>
            <el-table-column prop="createTime" label="注册时间"  align="center"></el-table-column>
            <!-- <el-table-column label="操作"  align="center" width="200">
                <template #default="scope">
                    <el-button type="primary" @click="addOne(scope.row)">编辑</el-button>
                </template>
            </el-table-column> -->
        </el-table>
    </div>
    <!-- 页码 -->
    <div class="flex justify-center items-end w-full h-16 mt-2">
        <el-pagination
            v-model:current-page="pageForm.currentPage"
            v-model:page-size="pageForm.pageSize"
            :page-sizes="[10, 20, 30, 40]"
            :background="background"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageForm.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            />
            <el-switch v-model="background" class="ml-2" />
    </div>
    <!-- 进行编辑添加 -->
    <el-dialog v-model="dialogFormVisible" title="编辑信息" width="300">
        <el-form :model="addEditForm" label-width="80px">
            <el-form-item label="姓名">
                <el-input v-model="addEditForm.name" placeholder="姓名" />
            </el-form-item>
            <el-form-item label="性别">
                <el-select v-model="addEditForm.sex" placeholder="性别">
                    <el-option label="男" value="0" />
                    <el-option label="女" value="1" />
                </el-select>
            </el-form-item>
            <el-form-item label="电话">
                <el-input v-model="addEditForm.phone" placeholder="医生电话" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="addEdit">确定</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
    <!-- 进行查看详情 -->
    <el-dialog v-model="dialogInfoVisible" title="用户详情" width="300">
        <el-form :model="addEditForm" label-width="80px">
            <el-form-item label="姓名">
                <el-input v-model="addEditForm.doctorName" placeholder="姓名" />
            </el-form-item>
            <el-form-item label="性别" placeholder="请选择">
               <el-select v-model="addEditForm.sex" >
                    <el-option label="男" value="0" />
                    <el-option label="女" value="1" />
                </el-select>
            </el-form-item>
            <el-form-item label="电话">
                <el-input v-model="addEditForm.phone" placeholder="医生电话" />
            </el-form-item>
        </el-form>
    </el-dialog>

</template>

<script setup>
import { ref, reactive, computed} from "vue";
import { ElMessageBox, ElMessage } from 'element-plus'
import { getList, add, del } from "@/api/user/patient.js"
// 表数据区
const tableData = ref([
    {id: 1, name: "张三", sex: "男", email: "123456@qq.com",phone: "123456789",  createTime: "2024-09-09"},
    {id: 2, name: "李四", sex: "男", email: "123456@qq.com",phone: "123456789",  createTime: "2024-09-09"},
    {id: 3, name: "王五", sex: "男", email: "123456@qq.com",  phone: "123456789", createTime: "2024-09-09"},
])
// 添加编辑
const dialogFormVisible = ref(false)
// 详情
const dialogInfoVisible = ref(false)
// 搜索表
const formInline = reactive({
    name: "",
    sex: "",
    phone: "",
})
// 页数
const pageForm = reactive({
    currentPage: 1,
    pageSize: 10,
    total: 50
})
// 背景
const background = ref(false)
// 搜索条件
const pageSearch = computed(() => {
  return {
    page: pageForm.currentPage,
    limit: pageForm.pageSize,
    name: formInline.name,
    phonenumber: formInline.phonenumber,
  }
})
// 编辑或者添加
var addEditForm = reactive({
    name: "",
    sex: "",
    phone: "",
})
// 选择复选框
let idArr = []
const selected = (data) => {
  idArr = []
  data.forEach((value) => {
    idArr.push(value)
  })
  console.log(data)
  console.log(idArr)
}
initData()
// 初始化方法
function initData(){
    getList(pageSearch.value).then(res =>{
        console.log(res)
        // 初始化表格数据
        tableData.value = res.data.list
        pageForm.total = res.data.totalCount
        pageForm.currentPage = res.data.currPage
        pageForm.pageSize = res.data.pageSize
    })
}


// 点击搜索
const onSearch = () => {
//   console.log(pageSearch)
    initData()
}
// 获取详细信息
function getInfo(row){
    dialogInfoVisible.value = true
    console.log(row)
}

const handleSizeChange = (val) => {
  // console.log(`${val} items per page`)
  initData()
}
const handleCurrentChange = (val) => {
  // console.log(`current page: ${val}`)
  initData()
}
// 删除
function delMore(){
    if(idArr.length == 0){
        ElMessage.error("请选择")
        return
    }
    alert(idArr.length)
    // 进行删除
    ElMessage.success("删除成功")
}
// 进行添加
function addOne(n){
    console.log(n)
    dialogFormVisible.value = true
    if(n == 0){
        console.log("进行添加")
        addEditForm = {name: "", sex: "", phone: ""}
    }else{
        console.log("进行编辑")
        addEditForm = computed(()=>{
            return {
                name: n.name,
                sex: n.sex,
                phone: n.phone,
            }
        })
    }
}

// 确定
function addEdit(){
    dialogFormVisible.value = false
    console.log("点击了确定")
    console.log(addEditForm)
    ElMessage.success("添加成功")
}
</script>

<style scoped>

</style>